﻿using System;
using System.Linq;
using System.Windows.Forms;
using SenseTimeTracker.BusinessLayer;
using SenseTimeTracker.Main;
using SenseTimeTracker.Misc;
using SenseTimeTracker.Properties;
using StedySoft.SenseSDK;
using StedySoft.SenseSDK.DrawingCE;

namespace SenseTimeTracker.Categories
{
	/// <summary>
	/// Class implements the category slider panel
	/// </summary>
	public class CategoriesSliderPanel : CommonSliderPanel<Category>
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="CategoriesSliderPanel"/> class.
		/// </summary>
		/// <param name="form">
		/// The form the splider panel belongs to.
		/// </param>
		public CategoriesSliderPanel(SenseTimeTrackerForm form)
			: base(form, "CategoriesSmallIcon.png", "CategoriesLargeIcon.png", MenuEnty.New  | MenuEnty.Edit | MenuEnty.Remove)
		{
			sorting = Persistance.Instance.CategoriesSorting;
			sortColumns = SortColumn.Name;
		}

		/// <summary>
		/// Gets Name of the panel.
		/// </summary>
		public override string Name
		{
			get { return Resources.PanelCategoriesName; }
		}

		/// <summary>
		/// Binds the category items to the sense list control
		/// </summary>
		/// <param name="senseList">
		/// The sense list control.
		/// </param>
		public override void Bind(SenseListControl senseList)
		{
			base.Bind(senseList);

			senseList.AddItem(new SensePanelDividerItem(string.Empty, Resources.CategoriesExplanation));

			var filteredCategories = from category in Category.Categories where category.Id != DatabaseManager.UnknownTaskCategoryId select category;
			SortFilter(ref filteredCategories);
			foreach (Category category in filteredCategories)
			{
				senseList.AddItem(new CategoryDisplayItem
				{
					Category = category,
				});
			}
		}

		/// <summary>
		/// Binds the filter to the sense list control
		/// </summary>
		public override void BindFilter()
		{
			BindFilter(Resources.CategoriesFilterHeader);
		}

		/// <summary>
		/// Handles menu item new entry click event
		/// </summary>
		/// <param name="sender">
		/// The sender of the vent.
		/// </param>
		/// <param name="e">
		/// The event arguments.
		/// </param>
		protected override void MenuItemNewEntry_Click(object sender, EventArgs e)
		{
			using (new DialogHelper(ownedForm))
			{
				CategoryEditForm form = new CategoryEditForm
				{
					Category = new Category { Id = Guid.NewGuid() }
				};

				if (form.ShowDialog() == DialogResult.OK)
				{
					form.Category.Insert();
					Bind(senseListControl);
				}
			}
		}

		/// <summary>
		/// Handles menu item editw entry click event
		/// </summary>
		/// <param name="sender">
		/// The sender of the vent.
		/// </param>
		/// <param name="e">
		/// The event arguments.
		/// </param>
		protected override void MenuItemEditEntry_Click(object sender, EventArgs e)
		{
			CategoryDisplayItem item = senseListControl.FocusedItem as CategoryDisplayItem;
			if (item != null)
			{
				using (new DialogHelper(ownedForm))
				{
					CategoryEditForm form = new CategoryEditForm
					{
						Category = item.Category,
					};

					if (form.ShowDialog() == DialogResult.OK)
					{
						form.Category.Update();
						Bind(senseListControl);
					}
				}
			}
		}

		/// <summary>
		/// Handles menu item remove entry click event
		/// </summary>
		/// <param name="sender">
		/// The sender of the vent.
		/// </param>
		/// <param name="e">
		/// The event arguments.
		/// </param>
		protected override void MenuItemRemoveEntry_Click(object sender, EventArgs e)
		{
			CategoryDisplayItem item = senseListControl.FocusedItem as CategoryDisplayItem;
			if (item != null)
			{
				if (item.Category.Id == DatabaseManager.UnknownTaskCategoryId)
				{
					SenseAPIs.SenseMessageBox.Show(Resources.DialogMessageCategoryRemoveWarning, Resources.DialogHeaderRemoveCategory, SenseMessageBoxButtons.OK);
				}
				else if (SenseAPIs.SenseMessageBox.Show(Resources.DialogDeleteQuestion, Resources.DialogHeaderRemoveCategory, SenseMessageBoxButtons.YesNo) == DialogResult.Yes)
				{
					Category.Delete(item.Category.Id);
					Bind(senseListControl);
				}
			}
		}
	}
}
